// 引入依赖

const ts = require('rollup-plugin-typescript2')
const Json = require('@rollup/plugin-json')
const resolvePlugin = require('@rollup/plugin-node-resolve') // 解析第三方插件
const path = require('path')
const { ModuleResolutionKind } = require('typescript')

// 获取文件路径
const packagesDir = path.resolve(__dirname,'packages')
const packageDir = path.resolve(packagesDir , process.env.TARGET)
//打包获取到每个包的项目配置
const resolve = p => path.resolve(packageDir,p)
const pkg = require(resolve('package.json'))
const name = path.basename(packageDir) // 获取末尾包名
// 创建映射表
const outputOptions = {
   'esm-bundler':{
    file: resolve(`dist/${name}.esm-bundler.js`),
    format:'es'
   },
   'cjs':{
    file: resolve(`dist/${name}.cjs.js`),
    format:'cjs'
   },
   'global':{
    file: resolve(`dist/${name}.global.js`),
    format:'iife'
   }
}
const options = pkg.buildOptions
function createConfig(format , output){
   output.name = options.name
   output.sourcemap = true
   return {
      input : path.resolve(packageDir , 'src/index.ts'),
      output,
      plugins:[
         Json(),
         ts({
            tsconfig:path.resolve(__dirname,'tsconfig.json')
         }),
         resolvePlugin()
      ]
   }
}
// 拿到配置项数组开始打包 导出
 module.exports = options.formats.map(format => createConfig(format,outputOptions[format]))
